home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libsurf / fogdeck.c < prev    next >
C/C++ Source or Header  |  1991-08-08  |  3KB  |  121 lines

  1. /*
  2.  * fogdeck.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: fogdeck.c,v 4.0 91/07/17 14:40:28 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    fogdeck.c,v $
  19.  * Revision 4.0  91/07/17  14:40:28  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "atmosphere.h"
  24. #include "fogdeck.h"
  25.  
  26. Fogdeck *
  27. FogdeckCreate(alt, offset, scale, chaoscale, octaves, color, trans)
  28. Float alt, offset, chaoscale;
  29. Vector *scale;
  30. int octaves;
  31. Color *color, *trans;
  32. {
  33.     Fogdeck *fogdeck;
  34.     static void ComputeFogdeck();
  35.  
  36.     fogdeck = (Fogdeck *)Malloc(sizeof(Fogdeck));
  37.  
  38.     fogdeck->alt = alt;
  39.     fogdeck->octaves = octaves;
  40.     fogdeck->scale = *scale;
  41.     fogdeck->chaoscale = chaoscale;
  42.     fogdeck->offset = offset;
  43.  
  44.     if (color == (Color *)NULL)
  45.         fogdeck->color.r = fogdeck->color.g = fogdeck->color.b = 0.;
  46.     else
  47.         fogdeck->color = *color;
  48.     if (trans == (Color *)NULL)
  49.         fogdeck->trans.r = fogdeck->trans.g = fogdeck->trans.b =
  50.             FAR_AWAY;
  51.     else {
  52.         fogdeck->trans = *trans;
  53.     }
  54.     return fogdeck;
  55. }
  56.  
  57. /*
  58.  * Add fogdeck to the given color.
  59.  */
  60. void
  61. FogdeckApply(fogdeck, ray, pos, dist, color)
  62. Fogdeck *fogdeck;
  63. Ray *ray;
  64. Vector *pos;
  65. Float dist;
  66. Color *color;
  67. {
  68.     Float atten, hitdist, density;
  69.     Color trans;
  70.     Vector endp;
  71.     extern Float ExpAtten(), PAChaos();
  72.  
  73.     /*
  74.      * Find distance from origin at which ray strikes
  75.      * z = fogdeck->alt plane
  76.      */
  77.     if (abs(ray->dir.z) < EPSILON)
  78.         return;
  79.     hitdist = (fogdeck->alt - ray->pos.z) / ray->dir.z;
  80.     if (hitdist < EPSILON || hitdist > dist)
  81.         return;
  82.     /*
  83.      * Compute ray endpoint
  84.      */
  85.     VecAddScaled(ray->pos, hitdist, ray->dir, &endp);
  86.  
  87.     /*
  88.      * Modify transmissivity based on point of
  89.      * intersection.
  90.      */
  91.     endp.x *= fogdeck->scale.x;
  92.     endp.y *= fogdeck->scale.y;
  93.     endp.z *= fogdeck->scale.z;
  94.  
  95.     density = fogdeck->offset +
  96.             fogdeck->chaoscale * PAChaos(&endp, fogdeck->octaves);
  97.     if (density < EPSILON)
  98.         density = HUGE;
  99.     else
  100.         density = 1. / density;
  101.  
  102.     trans = fogdeck->trans;
  103.     ColorScale(density, trans, &trans);
  104.  
  105.     dist -= hitdist;
  106.     
  107.     atten = ExpAtten(dist, trans.r);
  108.  
  109.     if (trans.r == trans.g &&
  110.         trans.r == trans.b) {
  111.         ColorBlend(color, &fogdeck->color, atten, 1. - atten);
  112.         return;
  113.     }
  114.     color->r = atten*color->r + (1. - atten) * fogdeck->color.r;
  115.  
  116.     atten = ExpAtten(dist, trans.g);
  117.     color->g = atten*color->g + (1. - atten) * fogdeck->color.g;
  118.     atten = ExpAtten(dist, trans.b);
  119.     color->b = atten*color->b + (1. - atten) * fogdeck->color.b;
  120. }
  121.